package pl.jtb.ctl.tabory.ui.boe.selection;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import pl.jtb.ctl.tabory.dao.data.vo.CompositionVO;
import pl.jtb.ctl.tabory.dao.data.vo.VagonVO;
import pl.jtb.ctl.tabory.ui.editors.EditorStateSL;
import pl.jtb.ctl.tabory.ui.selection.ISelection;

public class VagonSelection implements ISelection {

    private static final long serialVersionUID = 1L;

    private List<VagonVO> listVagons;
    private VagonVO selectedVagon;
    private List<CompositionVO> listCompositions;
    private EditorStateSL editorState;
    private Map<CompositionVO, List<VagonVO>> mapCompositionVagons;

    private Map<VagonVO, CompositionVO> mapVagonComposition;

    public List<VagonVO> getListVagons() {
        return listVagons;
    }

    public void setListVagons(List<VagonVO> listVagons) {
        this.listVagons = listVagons;
    }

    public VagonVO getSelectedVagon() {
        return selectedVagon;
    }

    public void setSelectedVagon(VagonVO selectedVagon) {
        this.selectedVagon = selectedVagon;
    }

    public List<CompositionVO> getListCompositions() {
        return listCompositions;
    }

    public void setListCompositions(List<CompositionVO> listCompositions) {
        this.listCompositions = listCompositions;
    }

    public EditorStateSL getEditorState() {
        return editorState;
    }

    public void setEditorState(EditorStateSL editorState) {
        this.editorState = editorState;
    }

    public void addComposition(final CompositionVO composition) {
        listCompositions.add(composition);
    }

    public void removeVagon(final CompositionVO composition) {
        listCompositions.remove(composition);
    }

    public void setMapCompositionVagons(Map<CompositionVO, List<VagonVO>> mapCompositionVagons) {
        this.mapCompositionVagons = mapCompositionVagons;
        mapVagonComposition = new LinkedHashMap<VagonVO, CompositionVO>();
        for (Entry<CompositionVO, List<VagonVO>> entry : mapCompositionVagons.entrySet()) {
            CompositionVO composition = entry.getKey();
            for (VagonVO vagon : entry.getValue()) {
                mapVagonComposition.put(vagon, composition);
            }
        }
    }

    public Map<CompositionVO, List<VagonVO>> getMapCompositionVagons() {
        return mapCompositionVagons;
    }

    public CompositionVO getCompositionForVagon(final VagonVO vagon) {
        if (mapVagonComposition != null) {
            for (Entry<VagonVO, CompositionVO> entry : mapVagonComposition.entrySet()) {
                VagonVO key = entry.getKey();
                if (key.getId().equals(vagon.getId())) {
                    return entry.getValue();
                }
            }
        }
        return null;
    }

    @Override
    public String toString() {
        return "VagonSelection [listVagons=" + listVagons + ", selectedVagon=" + selectedVagon + ", listCompositions=" + listCompositions + ", editorState=" + editorState + ", mapCompositionVagons=" + mapCompositionVagons
                + ", mapVagonComposition=" + mapVagonComposition + "]";
    }

}
